home *** CD-ROM | disk | FTP | other *** search
- Path: newsroom.hitc.com!usenet
- From: psand@eos.hitc.com (G. Patrick Sand)
- Newsgroups: comp.lang.c++
- Subject: Re: what happens w/delete called twice ?
- Date: 23 Feb 1996 17:48:27 GMT
- Organization: Hughes Aircraft (EOSDIS)
- Message-ID: <4gkulb$gkr@newsroom.hitc.com>
- References: <kcc.423.0EE12CF6@interaccess.com>
- NNTP-Posting-Host: 155.157.118.56
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.99.3
-
- In article <kcc.423.0EE12CF6@interaccess.com>, kcc@interaccess.com
- says...
- >
- >Hello,
- >
- > Can someone tell me... what happens when delete is called twice
- for the
- > same
- >memory deallocation [SNIP!]
-
- You have probably caused yourself a lot of pain...and may be creating a
- memory allocation error to boot...
-
- Unless you (or your compiler--very unlikely) sets to pointer to zero
- after
- deallocating with delete, you could have the following situations occur:
-
- 1. in a multi-threaded environment, you may have just "freed" up some
- memory
- which somebody else has allocated, making it likely that memory
- corruption will occur soon.
-
- 2. in a multi-user environment, a similar thing may happen between
- distinct processes.
-
- 3. if you compiler's run-time library (or a 3rd-party memory manager) is
- clever enough to detect when you attempt to deallocate memory at a
- nonzero address that is already deallocated, you will get a run-time
- exception--not a nice thing to have happen... If they don't do it, you
- will have lots of fun diagnosing and debugging this one...
-
- As I understand things (subject to brainlocks now and then...), you can
- always delete a pointer that has address 0 as its' value--this C++
- loophole is to allow you to gracefully handling deleting "an object which
- wasn't fully constructed by the constructor"--which should return a zero
- pointer value...
-
- So, I guess you either force the pointer to 0 after deleting the first
- time, or (better yet) only delete the object once. Check out Effective
- c++ by Scott Meyers (ISBN 0-201-56364-9) Items 6 and 7; you might also
- want to check out his new book (I haven't gotten it yet). Look at
- http://www.aw.com
- for the Addison-Wesley home page and cruise on down the the
- Addison-Wesley Professional Computing Series pages...
-
-
- --
- G. Patrick Sand
- psand@eos.hitc.com
- PatSand@aol.com
- (301) 925-0791
- "Travel Light But Right..."
- Microsoft Network is prohibited from redistributing
- this work in any form, in whole or in part. License
- to distribute this individual post is available to Microsoft
- for $999. Posting without permission constitutes an
- agreement to these terms...gps
-
-